﻿using Andy.Web.SanPhamServiceReference;
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vdc.Grid;

namespace Andy.Web.admin
{
    public partial class ht_qlsp : VdcInc.vdcAJAXPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string number = "#,###;-#,###;0";
            string value = ddlDanhMuc.SelectedValue == "" ? "0" : ddlDanhMuc.SelectedValue;
            if (!IsPostBack)
            {

                loadDDL();
                loadGid(Convert.ToInt64(value),"");
                
            }
        }
        public void loadDDL()
        {
            using (var spService = new SanPhamServiceReference.SanPhamServiceClient())
            {
                var lstLoaiSanPham = spService.DanhSachLoaiSanPham();
                var first_lsp = new LoaiSanPham();
                first_lsp.Id = 0;
                first_lsp.Ten = "=== Tất cả ===";
                lstLoaiSanPham.Insert(0, first_lsp);
                ddlDanhMuc.DataSource = lstLoaiSanPham;
                ddlDanhMuc.DataValueField = "Id";
                ddlDanhMuc.DataTextField = "Ten";
                ddlDanhMuc.DataBind();
                ddlDanhMuc.SelectedValue = "0";
            }
        }

        public void loadGid(long idloai,string tukhoa)
        {
            using (var spService = new SanPhamServiceReference.SanPhamServiceClient())
            {
                var lst = spService.TimKiemSanPham(idloai, tukhoa);
                if (lst != null)
                {
                    grid1.DataSource = lst;
                    grid1.DataBind();
                    
                }
                else
                {
                    grid1.DataSource = null;
                    grid1.DataBind();
                }

                
            }
            
        }

        protected void grid1_RowDataBound(object sender, Vdc.Grid.GridRowEventArgs e)
        {
            if (e.Row.RowType == GridRowType.DataRow && grid1.RowsInViewState.Count > 1)
            {
                if (grid1.RowsInViewState.Count > e.Row.RowIndex)
                {
                    GridDataControlFieldCell cell = e.Row.Cells[0] as GridDataControlFieldCell;
                    CheckBox ckBox = cell.FindControl("ChkID") as CheckBox;
                    HiddenField hiddenField = cell.FindControl("ProductName") as HiddenField;

                    GridDataControlFieldCell cellInViewState = grid1.RowsInViewState[e.Row.RowIndex].Cells[0] as GridDataControlFieldCell;
                    CheckBox ckBoxInViewState = cellInViewState.FindControl("ChkID") as CheckBox;
                    HiddenField hiddenFieldInViewState = cellInViewState.FindControl("ProductName") as HiddenField;

                    ckBox.Checked = ckBoxInViewState.Checked;
                    hiddenField.Value = hiddenFieldInViewState.Value;
                }
            }
        }

        protected void VdcButton1_Click1(object sender, EventArgs e)
        {
            string selectedOrderIds = "";

            for (int i = 0; i < grid1.RowsInViewState.Count; i++)
            {
                GridDataControlFieldCell cell = grid1.RowsInViewState[i].Cells[0] as GridDataControlFieldCell;
                CheckBox chk = cell.FindControl("ChkID") as CheckBox;
                HiddenField hiddenField = cell.FindControl("ProductName") as HiddenField;
                if (chk.Checked.ToString() != chk.ToolTip)
                {
                    selectedOrderIds += hiddenField.Value + "-" + chk.Checked.ToString() + ",";
                }
            }
            List<string> c = selectedOrderIds.Split(',').ToList();
            List<string> item = null;
            List<SanPham> lst = new List<SanPham>();
            foreach (string d in c)
            {
                if (d != "")
                {
                    item = d.Split('-').ToList();
                    SanPham sp = new SanPham();
                    sp.Id = Convert.ToInt64(item[0]);
                    sp.CheckNoiBat = Convert.ToBoolean(item[1]);
                    lst.Add(sp);
                }

            }
            using (var spService = new SanPhamServiceReference.SanPhamServiceClient())
            {
                var resultCapNhat = spService.CapNhatSanPhamNoiBat(lst);
                if (resultCapNhat == null) resultCapNhat = "Thất bại";
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "SymbolError", "alert('Cập nhật " + resultCapNhat + "')", true);
            }
        }

        public string vXoaSanPham(string strSanPhamId)
        {
            using (var spService = new SanPhamServiceReference.SanPhamServiceClient())
            {
                if (!string.IsNullOrEmpty(strSanPhamId))
                {
                    var sp = new SanPham();
                    sp.Id = Convert.ToInt64(strSanPhamId);
                    var result = spService.CapNhatSanPham(sp);
                    return "Xóa " + result;
                }
                return "Xóa thất bại";
            }
        }

        protected void btTimKiem_Click(object sender, EventArgs e)
        {
            long idloai = Convert.ToInt64(ddlDanhMuc.SelectedValue);
            string tukhoa = txtTuKhoa.Text;
            //hdfValue_DanhMuc.Value = ddlDanhMuc.SelectedValue.ToString();
            loadGid(idloai, tukhoa);
        }
    }
}